package com.atguigu.iterator;

import java.util.Iterator;
import java.util.List;

/**
 * @program: design-patterns
 * @author: yangshen
 * @description:
 * @create: 2022-03-07 12:37
 **/
public class OutputImpl {
    /**
     * 学院的集合
     */
    List<College> collegeList;

    public OutputImpl(List<College> collegeList) {
        this.collegeList = collegeList;
    }

    //遍历所有的学院，然后调用printDepartment 输出各个学院下的系
    public void printCollege(){
        //从collegeList取出所有的学院 (java中的list已经实现了Iterator接口)
        Iterator<College> iterator = collegeList.iterator();
        while (iterator.hasNext()){
            //取出一个学院
            College college = iterator.next();
            System.out.println("---------------------"+college.getName()+"------------------");
            //取出学院对应的迭代器
            Iterator collegeIterator = college.createIterator();
            //使用学院的迭代器输出所有系
            printDepartment(collegeIterator);
        }
    }


    /**
     * 使用学院对应的迭代器输出 系
     * @param iterator 学院对应的迭代器
     */
    public void printDepartment(Iterator iterator){
        while (iterator.hasNext()){
            Department department =(Department) iterator.next();
            System.out.println(department.getName());
        }
    }
}
